<template>
  <div class="simple-calendar">
    <div class="header">
      <button @click="previousMonth">&lt;</button>
      <h2>{{ currentMonth }}</h2>
      <button @click="nextMonth">&gt;</button>
    </div>
    <div class="days">
      <div v-for="day in days" :key="day" class="day">{{ day }}</div>
      <div v-for="blank in firstDayOffset" :key="blank" class="blank"></div>
      <div v-for="date in currentMonthDates" :key="date" @click="selectDate(date)" class="date">{{ date }}</div>
      <table>
        <tr><td></td></tr>
      </table>
    </div>
  </div>
</template>

<script>
import fecha from '@/utils/date';
export default {
  data() {
    return {
      currentDate: new Date(),
    };
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', {month: 'long', year: 'numeric'});
    },
    days() {
      return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    },
    firstDayOffset() {
      console.log('this.currentDate',  fecha.format(this.currentDate,'yyyy-MM-dd'))
      const firstDay = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), 1).getDay();
      return Array.from({length: firstDay}, (_, index) => index);
    },
    currentMonthDates() {
      const lastDay = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() + 1, 0).getDate();
      return Array.from({length: lastDay}, (_, index) => index + 1);
    },
  },
  methods: {
    previousMonth() {
      this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() - 1, 1);
    },
    nextMonth() {
      this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() + 1, 1);
    },
    selectDate(date) {
      console.log('Selected Date:', date);

      Array.apply(null, {length: 4}).map((_, n) => n)
      // 处理日期选择的逻辑，可以根据需求进行定制
    },

  },
  mounted() {
    // console.log('yyyy-MM-dd:',  fecha.format(new Date(),'yyyy-MM-dd'));
  }
};
</script>

<style scoped>
.simple-calendar {
  font-family: 'Arial', sans-serif;
  max-width: 300px;
  margin: auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #4CAF50;
  color: white;
  padding: 10px;
}

.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.day, .date {
  text-align: center;
  padding: 10px;
  cursor: pointer;
}

.day {
  background-color: #4CAF50;
  color: white;
}

.date {
  background-color: #f2f2f2;
  border-radius: 5px;
  transition: background-color 0.3s ease;
}

.date:hover {
  background-color: #ddd;
}

.blank {
  visibility: hidden;
}
</style>
